<template>
  <div class="notice-container">
    <div class="page-header">
      <h1 class="page-title">
        <el-icon class="title-icon"><BellFilled /></el-icon>
        系统公告
      </h1>
      <p class="page-subtitle">了解最新的系统动态和活动信息</p>
    </div>

    <div class="content">
      <el-timeline v-loading="loading">
        <el-timeline-item
          v-for="notice in notices"
          :key="notice.id"
          :timestamp="formatTime(notice.createTime)"
          placement="top"
        >
          <el-card class="notice-card" @click="viewNotice(notice)">
            <template #header>
              <div class="card-header">
                <span class="notice-title">
                  <el-tag
                    :type="getNoticeTypeTag(notice.type)"
                    size="large"
                    class="notice-tag"
                  >
                    {{ getNoticeTypeName(notice.type) }}
                  </el-tag>
                  {{ notice.title }}
                </span>
              </div>
            </template>
            <div class="notice-content">
              {{ notice.content }}
            </div>
          </el-card>
        </el-timeline-item>
      </el-timeline>

      <div class="pagination-wrapper" v-if="total > 0">
        <el-pagination
          v-model:current-page="page"
          v-model:page-size="size"
          :total="total"
          layout="total, prev, pager, next"
          @current-change="fetchNotices"
        />
      </div>

      <el-empty 
        v-if="!loading && notices.length === 0" 
        description="暂无公告"
        :image-size="150"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { BellFilled } from '@element-plus/icons-vue'
import dayjs from 'dayjs'
import { getNoticeList } from '@/api/notice'

const router = useRouter()

const loading = ref(false)
const notices = ref([])
const page = ref(1)
const size = ref(10)
const total = ref(0)

const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm')
}

const getNoticeTypeName = (type) => {
  const types = ['系统公告', '活动公告', '维护公告']
  return types[type] || '系统公告'
}

const getNoticeTypeTag = (type) => {
  const tags = ['', 'success', 'warning']
  return tags[type] || ''
}

const fetchNotices = async () => {
  loading.value = true
  try {
    const res = await getNoticeList({ page: page.value, size: size.value })
    notices.value = res.data.records || []
    total.value = res.data.total || 0
  } catch (error) {
    console.error('获取公告列表失败：', error)
    ElMessage.error('获取公告列表失败')
  } finally {
    loading.value = false
  }
}

const viewNotice = (notice) => {
  ElMessage.info({
    message: notice.content,
    duration: 5000,
    showClose: true
  })
}

onMounted(() => {
  fetchNotices()
})
</script>

<style scoped>
.notice-container {
  min-height: 100%;
  padding: 30px 20px;
  animation: fadeIn 0.6s ease-out;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
}

.page-title {
  font-size: 32px;
  font-weight: 800;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.title-icon {
  font-size: 36px;
  color: #667eea;
}

.page-subtitle {
  font-size: 16px;
  color: #666;
  margin: 0;
}

.content {
  max-width: 1000px;
  margin: 0 auto;
  background: white;
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.12);
  border: 1px solid rgba(102, 126, 234, 0.1);
}

.content :deep(.el-timeline) {
  padding-left: 20px;
}

.content :deep(.el-timeline-item__tail) {
  border-left: 2px solid #e4e7ed;
}

.content :deep(.el-timeline-item__node) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: 3px solid #fff;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

.content :deep(.el-timeline-item__timestamp) {
  color: #999;
  font-size: 13px;
  font-weight: 500;
}

.notice-card {
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid #f0f2f5;
  margin-bottom: 20px;
}

.notice-card:hover {
  box-shadow: 0 12px 32px rgba(102, 126, 234, 0.2);
  transform: translateY(-4px);
  border-color: #667eea;
}

.notice-card :deep(.el-card__header) {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(255, 255, 255, 1) 100%);
  border-bottom: 1px solid #f0f2f5;
  padding: 20px 24px;
}

.notice-card :deep(.el-card__body) {
  padding: 24px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.notice-title {
  font-weight: 600;
  font-size: 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  color: #333;
  transition: color 0.3s;
}

.notice-card:hover .notice-title {
  color: #667eea;
}

.notice-tag {
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 20px;
}

.notice-content {
  color: #666;
  line-height: 1.8;
  white-space: pre-wrap;
  font-size: 15px;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid #f0f2f5;
}

.el-empty {
  padding: 60px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .notice-container {
    padding: 20px 12px;
  }

  .page-title {
    font-size: 24px;
  }

  .title-icon {
    font-size: 28px;
  }

  .page-subtitle {
    font-size: 14px;
  }

  .content {
    padding: 24px 16px;
    border-radius: 12px;
  }

  .notice-card :deep(.el-card__header),
  .notice-card :deep(.el-card__body) {
    padding: 16px;
  }

  .notice-title {
    font-size: 16px;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
}
</style>

